std::visit 您所在的位置:网站首页 std variant std::visit

std::visit

2024-03-01 23:57| 来源: 网络整理| 查看: 265

 C++ Compiler support Freestanding and hosted Language Standard library Standard library headers Named requirements Feature test macros (C++20) Language support library Concepts library (C++20) Metaprogramming library (C++11) Diagnostics library General utilities library Strings library Containers library Iterators library Ranges library (C++20) Algorithms library Numerics library Localizations library Input/output library Filesystem library (C++17) Regular expressions library (C++11) Concurrency support library (C++11) Technical specifications Symbols index External libraries [edit] Utilities library Language support Type support (basic types, RTTI) Library feature-test macros (C++20) Dynamic memory management Program utilities Coroutine support (C++20) Variadic functions is_constant_evaluated(C++20) is_within_lifetime(C++26) initializer_list(C++11) source_location(C++20) Debugging support is_debugger_present(C++26) breakpoint_if_debugging(C++26) breakpoint(C++26) Three-way comparison three_way_comparablethree_way_comparable_with(C++20)(C++20) strong_ordering(C++20) weak_ordering(C++20) partial_ordering(C++20) common_comparison_category(C++20) compare_three_way_result(C++20) compare_three_way(C++20) strong_order(C++20) weak_order(C++20) partial_order(C++20) compare_strong_order_fallback(C++20) compare_weak_order_fallback(C++20) compare_partial_order_fallback(C++20)    is_eqis_ltis_lteq(C++20)(C++20)(C++20) is_neqis_gtis_gteq(C++20)(C++20)(C++20) General utilities Date and time Function objects Formatting library (C++20) bitset hash(C++11) Relational operators (deprecated in C++20) rel_ops::operator!=rel_ops::operator>    rel_ops::operator= Integer comparison functions cmp_equalcmp_lesscmp_less_than(C++20)(C++20)(C++20)    cmp_not_equalcmp_greatercmp_greater_than(C++20)(C++20)(C++20) in_range(C++20) Swap and type operations swap ranges::swap(C++20) exchange(C++14) declval(C++11) to_underlying(C++23) forward(C++11) forward_like(C++23) move(C++11) move_if_noexcept(C++11) as_const(C++17) Common vocabulary types pair tuple(C++11) optional(C++17) any(C++17) variant(C++17) tuple_size(C++11) tuple_element(C++11) apply(C++17) make_from_tuple(C++17) expected(C++23) Elementary string conversions to_chars(C++17) from_chars(C++17) chars_format(C++17) to_chars_result(C++17) from_chars_result(C++17) [edit] std::variant Member functions variant::variant variant::~variant variant::operator= Observers variant::index variant::valueless_by_exception Modifiers variant::emplace variant::swap Visitation variant::visit(C++26) Non-member functions visit holds_alternative get get_if operator==operator!=operator=operator(C++20) swap Helper classes monostate bad_variant_access variant_size variant_alternative hash Helper objects variant_npos [edit]  Defined in header template constexpr /* see below */ visit( Visitor&& vis, Variants&&... vars ); (1) (since C++17) template constexpr R visit( Visitor&& vis, Variants&&... vars ); (2) (since C++20) template auto&& as-variant( std::variant& var ); (3) (exposition only*) template auto&& as-variant( const std::variant& var ); (4) (exposition only*) template auto&& as-variant( std::variant&& var ); (5) (exposition only*) template auto&& as-variant( const std::variant&& var ); (6) (exposition only*)

Applies the visitor vis (a Callable that can be called with any combination of types from variants) to the variants vars.

Given VariantBases as decltype(as-variant(std::forward(vars))... (a pack of sizeof...(Variants) types):

1) Invokes vis as if by

INVOKE(std::forward(vis),       std::get(std::forward(vars))...),

where indices is as-variant(vars).index().... 2) Invokes vis as if by

INVOKE(std::forward(vis),          std::get(std::forward(vars))...),

where indices is as-variant(vars).index()....

These overloads participate in overload resolution only if every type in VariantBases is a valid type. If the expression denoted by INVOKE or INVOKE(since C++20) is invalid, or the results of INVOKE or INVOKE(since C++20) have different types or value categories for different indices, the program is ill-formed.

3-6) The exposition-only as-variant function templates accept a value whose type can be deduced for std::variant (i.e. either std::variant or a type derived from std::variant), and return the std::variant value with the same const-qualification and value category. 3,4) Returns var. 5,6) Returns std::move(var). Contents 1 Parameters 2 Return value 3 Exceptions 4 Complexity 5 Notes 6 Example 7 Defect reports 8 See also [edit] Parameters vis - a Callable that accepts every possible alternative from every variant vars - list of variants to pass to the visitor [edit] Return value 1) The result of the INVOKE operation. The return type is the type obtained from applying decltype to the result. 2) Nothing if R is (possibly cv-qualified) void; otherwise the result of the INVOKE operation. 3-6) A std::variant value converted from var. [edit] Exceptions

Throws std::bad_variant_access if as-variant(vars_i).valueless_by_exception() is true for any variant vars_i in vars.

[edit] Complexity

When the number of variants is zero or one, the invocation of the callable object is implemented in constant time, i.e. it does not depend on the number of types can be stored in the variant.

If the number of variants is larger than one, the invocation of the callable object has no complexity requirements.

[edit] Notes

Let n be (1 * ... * std::variant_size_v), implementations usually generate a table equivalent to an (possibly multidimensional) array of n function pointers for every specialization of std::visit, which is similar to the implementation of virtual functions.

Implementations may also generate a switch statement with n branches for std::visit (e.g. the MSVC STL implementation uses a switch statement when n is not greater than 256).

On typical implementations, the time complexity of the invocation of vis can be considered equal to that of access to an element in an (possibly multidimensional) array or execution of a switch statement.

Feature-test macro Value Std Feature __cpp_lib_variant 202102L (C++17)(DR) std::visit for classes derived from std::variant [edit] Example Run this code #include #include #include #include #include #include   // the variant to visit using var_t = std::variant;   // helper constant for the visitor #3 template inline constexpr bool always_false_v = false;   // helper type for the visitor #4 template struct overloaded : Ts... { using Ts::operator()...; }; // explicit deduction guide (not needed as of C++20) template overloaded(Ts...) -> overloaded;   int main() { std::vector vec = {10, 15l, 1.5, "hello"};   for (auto& v: vec) { // 1. void visitor, only called for side-effects (here, for I/O) std::visit([](auto&& arg){ std::cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有